import { existsSync, mkdirSync, createWriteStream, readFileSync } from "fs";
import fetch from "node-fetch";
import { dirname } from "path";

const images = JSON.parse(String(readFileSync("./images.json")));

(async () => {
  for (const imageId of Object.keys(images)) {
    const url = `${(images as any)[imageId]}`;

    const target = `./public/images/${imageId.split("__").join("/")}.png`;

    if (existsSync(target)) {
      console.log(`found ${target}`);
      continue;
    }

    mkdirSync(dirname(target), { recursive: true });

    console.log(`sync ${url}`);
    const resp = await fetch(url);
    resp.body!.pipe(createWriteStream(target));
  }
})();
